﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterRevenueMS.Componnent.Entities.SysConfig;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.SysConfig.OperateLog;
using WaterRevenueMS.Core.Repository.SysConfig;
using System.ComponentModel.Composition;

namespace WaterRevenueMS.Core.Service.SysConfig.Impl
{
    [Export(typeof(IOperateLogService))]
    public class OperateLogService : CoreServiceBase, IOperateLogService
    {
        #region 属性

        [Import]
        public IOperateLogRepository OperateLogRepository { get; set; }

        //public OperateLogService(IOperateLogRepository pRepository)
        //{
        //    OperateLogRepository = pRepository;
        //}
        public IQueryable<OperateLog> OperateLogs
        {
            get { return OperateLogRepository.Entities; }
        }

        #endregion

        #region 公共方法

        public OperationResult Insert(OperateLogModel model)
        {
            var entity = new OperateLog
            {
                Area = model.Area,
                Controller = model.Controller,
                Action = model.Action,
                Description = model.Description,
                IPAddress = model.IPAddress,
                LoginName = model.LoginName,
                UserId = model.UserId,
                LogTime = model.LogTime
            };
            OperateLogRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Delete()
        {
            var entities = OperateLogRepository.Entities.Where(t => t.IsDeleted == false);
            foreach (var entity in entities)
            {
                entity.IsDeleted = true;
                OperateLogRepository.Update(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }

        #endregion
    }
}
